package com.example.personalcenter;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import com.example.personalcenter.databinding.ActivityMainBinding;
import com.example.personalcenter.fragment.HomeFragment;
import com.example.personalcenter.fragment.ProfileFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.bottomNav.setOnItemSelectedListener(new BottomNavigationView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(android.view.MenuItem item) {
                Fragment target;
                if (item.getItemId() == R.id.nav_home) {
                    target = new HomeFragment();
                } else if (item.getItemId() == R.id.nav_profile) {
                    target = new ProfileFragment();
                } else {
                    return false;
                }
                getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.container, target)
                        .commit();
                return true;
            }
        });

        if (savedInstanceState == null) {
            binding.bottomNav.setSelectedItemId(R.id.nav_home);
        }
    }
}